package com.pets.service.Impl;

import com.pets.dao.AnimalMapper;
import com.pets.dao.AppointmentMapper;

import com.pets.entity.Animal;
import com.pets.entity.Appointment;

import com.pets.enums.YesNoEnum;
import com.pets.qo.AppointmentAddQO;

import com.pets.service.AppointmentService;

import com.pets.util.PageData;
import com.pets.util.Result;

import com.pets.vo.UserAdoptRecodeVO;
import com.pets.vo.UserCoverAdoptRecodeVO;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Service
public class AppointmentServiceImpl implements AppointmentService {

    @Resource
    private AppointmentMapper appointmentMapper;

    @Resource
    private AnimalMapper animalMapper;


    //预约的添加操作
    public Result AppointmentAdd( AppointmentAddQO appointmentAddQO){
        Result result=new Result();
        Appointment appointment=new Appointment();
        appointment.setAid(appointmentAddQO.getaId());
        appointment.setApcurrenttime(new Date());
        appointment.setApphone(appointmentAddQO.getApPhone());
        appointment.setApstatus(0);
        appointment.setUid(appointmentAddQO.getuId());
        appointment.setAptime(appointmentAddQO.getApTime());
        appointment.setApreason(appointmentAddQO.getApReason());
        appointment.setApsuid(appointmentAddQO.getApSuId());
        Animal animal = animalMapper.selectByPrimaryKey(appointmentAddQO.getaId());
        animal.setAstatus(1);
        animalMapper.updateByPrimaryKey(animal);
        int i=appointmentMapper.insert(appointment);
        if(i >0){
            result.setCode(YesNoEnum.YES.getCode());
            result.setMsg("预约成功");
            return result;
        }else {
            result.setCode(YesNoEnum.NO.getCode());
            result.setMsg("预约失败");
            return result;
        }

    }

    @Override
    public List<UserAdoptRecodeVO> getUserAdoptRecode(int uId) {
        return appointmentMapper.getUserAdoptRecode(uId);
    }

    @Override
    public Result cancelApply(int apId) {
        Appointment appointment = appointmentMapper.selectByPrimaryKey(apId);
        appointment.setApstatus(2);
        int i = appointmentMapper.updateByPrimaryKey(appointment);
        Result result = new Result();
        if( i > 0){
            result.setCode(YesNoEnum.YES.getCode());
            result.setMsg("取消成功");
        }else {
            result.setCode(YesNoEnum.NO.getCode());
            result.setMsg("取消失败");
        }
        return result;
    }

    @Override
    public List<UserCoverAdoptRecodeVO> getUserCoverAdoptRecode(int uId) {
        return appointmentMapper.getUserCoverAdoptRecode(uId);
    }

    @Override
    public Result agreeApply(int apId) {
        Result result = new Result();
        Appointment appointment = appointmentMapper.selectByPrimaryKey(apId);
        appointment.setApstatus(1);
        int i=appointmentMapper.updateByPrimaryKey(appointment);
        if(i > 0){
            result.setCode(YesNoEnum.YES.getCode());
            result.setMsg("操作成功");
            return result;
        }else {
            result.setCode(YesNoEnum.NO.getCode());
            result.setMsg("操作失败");
            return result;
        }
    }

    @Transactional
    @Override
    public Result disagreeApply(int apId) {
        Result result = new Result();
        Appointment appointment = appointmentMapper.selectByPrimaryKey(apId);
        appointment.setApstatus(3);
        Animal animal = animalMapper.selectByPrimaryKey(appointment.getAid());
        animal.setAstatus(0);
        animalMapper.updateByPrimaryKey(animal);
        int i=appointmentMapper.updateByPrimaryKey(appointment);
        if(i > 0){
            result.setCode(YesNoEnum.YES.getCode());
            result.setMsg("操作成功");
            return result;
        }else {
            result.setCode(YesNoEnum.NO.getCode());
            result.setMsg("操作失败");
            return result;
        }
    }

    @Override
    public Result shelvesAnimal(int aId) {
        Result result = new Result();
        Animal animal = animalMapper.selectByPrimaryKey(aId);
        animal.setAstatus(1);
        int i= animalMapper.updateByPrimaryKey(animal);
        if(i > 0){
            result.setCode(YesNoEnum.YES.getCode());
            result.setMsg("操作成功");
            return result;
        }else {
            result.setCode(YesNoEnum.NO.getCode());
            result.setMsg("操作失败");
            return result;
        }

    }
}
